package com.mxchip.livestarmobile.mobile.state;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import java.util.Locale;

import androidx.annotation.NonNull;

/**
 *
 */

public class LanguageSettingUtil {

    // 单例模式-
    private static LanguageSettingUtil instance;

    private LanguageSettingUtil() {
    }

    // 检验自身是否被创建-
    public static LanguageSettingUtil get() {
        if (instance == null)
            throw new IllegalStateException("language setting not initialized yet");
        return instance;
    }

    public static String getSystemLanguage() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return Resources.getSystem().getConfiguration().getLocales().get(0).getLanguage();
        } else {
            return Resources.getSystem().getConfiguration().locale.getLanguage();
        }
    }

    // 初始化-
    public static void init() {
        if (instance == null) {
            instance = new LanguageSettingUtil();
        }
    }

    // 创建Configuration-
    private Configuration getUpdatedLocaleConfig(String paramString, @NonNull Context context) {

        Configuration localConfiguration = context.getResources().getConfiguration();
        localConfiguration.locale = getLocale(paramString);
        return localConfiguration;
    }

    // 得到系统配置文件目前的语言设置-
    public String getLanguage(@NonNull Context context) {
        String language = SPreference.getUserLocale();
        if (TextUtils.equals(language, "auto")) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                return Resources.getSystem().getConfiguration().getLocales().get(0).getLanguage();
            } else {
                return Resources.getSystem().getConfiguration().locale.getLanguage();
            }
        }
        return language;
    }

    public String getCountry(@NonNull Context context) {
        return getLocale(context).getCountry();
    }

    // 如果配置文件中没有语言设置-
    public Locale getLocale(@NonNull Context context) {
        return getLocale(getLanguage(context));
    }

    // 创建新Locale并覆盖原Locale-
    public Locale getLocale(String paramString) {
        Locale localLocale = new Locale(paramString);
        Locale.setDefault(localLocale);
        return localLocale;
    }

    // 刷新显示配置-
    public void refreshLanguage(@NonNull Context context) {
        String str = getLanguage(context);
        Resources localResources = context.getResources();
        if (!TextUtils.equals(localResources.getConfiguration().locale.getLanguage(), str)) {
            Configuration localConfiguration = getUpdatedLocaleConfig(str, context);
            DisplayMetrics localDisplayMetrics = localResources.getDisplayMetrics();
            localResources.updateConfiguration(localConfiguration, localDisplayMetrics);
        }
    }

    // 设置系统语言-
    public void saveLanguage(String paramString, @NonNull Context context) {
        SPreference.setUserLocale(paramString);
    }
}
